<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>vue动画封装</title>
  <script src="./js/vue2.js"></script>
  <style>
    /* 可以将这些css样式使用js动画替代 */
    /* .v-enter,
    .v-leave-to{
      opacity: 0;
    }
    .v-enter-active,
    .v-leave-active{
      transition: opacity 1s;
    } */
  </style>
</head>
<body>
  <div id="app">
      <!-- 自定义组件中传递show属性进去，向插槽传值 -->
      <fade :show="show">
        <div>hello</div>
      </fade>
      <!-- 传入h1 -->
      <fade :show="show">
        <h1>hello</h1>
      </fade>
    <button @click="handleClick">toggle</button>
  </div>
</body>
</html>
<script>

  // 将所有的动画封装在了一个组件当中，比较推荐的方法
  Vue.component('fade',{
    props:['show'],
    template:`
      <transition 
        @before-enter="handleBeforeEnter"
        @enter="handleEnter"
        @after-enter="handleAfterEnter"
        >
        <slot v-if="show"></slot>  
      </transition>
    `,
    methods:{
      handleBeforeEnter: function(el){
        console.log('before-enter');
        el.style.color = 'blue';
      },
      handleEnter:function(el,done){
        console.log('enter');
        el.style.color = 'red';
        done();
      },
      handleAfterEnter:function(el){
        el.style.color = 'green';
      }
    }
  });

  var app = new Vue({
    el: '#app',
    data: {
      show:true,
    },
    methods:{
      handleClick: function(){
        this.show = !this.show;
      }
    }
  });
</script>